import { service } from '@vimi/utils-api'
import { dateFormat } from '@/common/utils/common'
export const enumCustomerType = {
  org_farm: '家庭农场',
  org_large_breed: '种养殖大户',
}
export const enumUseType = [{ label: '固定资产贷款' }, { label: '流动资金贷款' }]
export const enumMarriageStatus = {
  1: '已婚',
  2: '未婚',
  3: '离异',
  4: '丧偶',
}

export const enumReviewApproval = {
  0: '未申报',
  1: '已申报',
  2: '评审中',
  3: '审核拒绝',
  4: '审核通过',
  5: '审核通过',
  6: '审核通过',
  7: '审核通过',
  9: '审核退回',
}
export const enumApproveStatus = {
  '-1': '退回',
  0: '未审批',
  1: '审批中',
  2: '审批通过',
  3: '审批拒绝',
}

// 获取省农担审批内容
export const getFollow = (relatedList, key = 'PROJECT_APPROVAL') =>
  relatedList.length
    ? service
        .get(
          `/api/credit/wf/follow/?wfKey=${key}&wfRelatedId=${relatedList
            .map(it => it.customer_related_id)
            .join(',')}`
        )
        .then(res => {
          return res?.data
        })
    : Promise.resolve({})

export const exportTableColumns = [
  { attrs: { label: '序号', prop: 'index' } },
  { attrs: { label: '放款银行名称', prop: 'claim_bank_name' } },
  { attrs: { label: '归属市', prop: 'address_name_city' } },
  { attrs: { label: '归属县', prop: 'address_name_county' } },
  { attrs: { label: '借款客户', prop: 'customer_name' } },
  { attrs: { label: '被担保人类型', prop: 'org_class_name' } },
  { attrs: { label: '所属行业', prop: 'parent_main_business' } },
  { attrs: { label: '联系电话', prop: 'corp_phone' } },
  { attrs: { label: '身份证号', prop: 'lender_idcard' } },
  { attrs: { label: '放款金额(万元)', prop: 'warranty_amount' } },
  { attrs: { label: '放款开始时间', prop: 'loadDate' } },
  { attrs: { label: '放款结束时间', prop: 'loadEndDate' } },
  { attrs: { label: '贷款期限', prop: 'warranty_period' } },

  { attrs: { label: '授信金额(万元)', prop: 'reply_funds' } },
  { attrs: { label: '授信开始时间', prop: 'loadDateCredit' } },
  { attrs: { label: '授信结束时间', prop: 'loadEndDateCredit' } },
  { attrs: { label: '授信期限', prop: 'period_credit' } },

  { attrs: { label: '贷款用途分类', prop: 'use_category_name' } },
  { attrs: { label: '贷款用途', prop: 'funds_use_dir' } },
  { attrs: { label: '借款合同号', prop: 'gua_contract' } },
  { attrs: { label: '借据号', prop: 'iou_no', align: 'center' } },
  { attrs: { label: '银行贷款利率', prop: 'load_rate' } },
  { attrs: { label: '担保费率', prop: 'warranty_rate' } },
  { attrs: { label: '保费是否客户承担', prop: 'isFeeCustomer' } },
  { attrs: { label: '销售收入(万元)', prop: 'sale_income' } },
  { attrs: { label: '资产总额(万元)', prop: 'assets_total' } },
  { attrs: { label: '从业人数', prop: 'emp_total' } },
  { attrs: { label: '收取其他费用', prop: 'other_cost' } },
  { attrs: { label: '收取企业保证金', prop: 'income_margin' } },
  { attrs: { label: '备注', prop: 'remark' } },
  { attrs: { label: '项目状态', prop: 'statusLabel' } },
]

export const dataAdapter = item => {
  item.createTime = dateFormat(item.create_time, 'yyyy-MM-dd')
  item.createInfo = `时间：${item.createTime} 提交人：${item.submit_person_name}`
  item.loadDate = dateFormat(item.load_date, 'yyyy-MM-dd')
  item.loadEndDate = dateFormat(item.load_end_date, 'yyyy-MM-dd')
  item.loadDateCredit = dateFormat(item.load_date_credit, 'yyyy-MM-dd')
  item.loadEndDateCredit = dateFormat(item.load_end_date_credit, 'yyyy-MM-dd')

  item.approveStatus =
    +(item.is_send || '') === 0 ? '未发送' : enumApproveStatus[item.approval_status]

  item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`

  item.statusLabel = enumReviewApproval[item.review_approval]
  return item
}

export const countMonth = (start, end) => {
  let month = 0
  if (start && end) {
    const startData = new Date(start)
    const endData = new Date(end)
    const startYear = startData.getFullYear()
    const endYear = endData.getFullYear()
    const startMonth = startData.getMonth() + 1
    const endMonth = endData.getMonth() + 1
    const startDay = startData.getDate()
    const endDay = endData.getDate()
    month = (endYear - startYear) * 12 + (endMonth - startMonth)
    if (month < 6) {
      month -= endDay >= startDay ? 0 : 1
    } else {
      month += endDay > startDay ? 1 : 0
    }
  }
  if (month === 5) {
    return 6
  }
  if (month === 37) {
    return 36
  }
  return month
}
